package com.example.yuanboot.controller.system;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.UUID;
import com.example.yuanboot.common.anno.AnonymousAccess;
import com.example.yuanboot.common.msg.BaseResultCode;
import com.example.yuanboot.common.msg.ObjectRestResponse;
import com.example.yuanboot.common.msg.ResponseUtils;
import com.example.yuanboot.service.common.IRedisService;
import com.google.common.collect.ImmutableMap;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 验证码
 *
 * @author qingyan 2021-08-26
 */
@RestController
@RequiredArgsConstructor
public class SysCaptchaController {

    private final IRedisService redisService;
    /**
     * 验证码生成
     */
    @GetMapping(value = "/captchaImage")
    @AnonymousAccess
    public ObjectRestResponse<Map> getCaptcha() {
        String uuid = UUID.fastUUID().toString();
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        redisService.set(uuid, lineCaptcha.getCode(), 180);
        return ResponseUtils.r(ImmutableMap.of("uuid", uuid, "img", lineCaptcha.getImageBase64Data()), BaseResultCode.SUCCESS);
    }
}
